package com.shang.dao.impl;

import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.shang.dao.ResultDao;
import com.shang.entity.Result;
import com.shang.entity.UserMark;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;
@Slf4j
@Component
public class ResultDaoImpl implements ResultDao {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<UserMark> getUserMark(String userName) {
        String collectionName="user_mark";
        Criteria criteria=Criteria.where("userName").is(userName);
        Query query=new Query(criteria);
        List<UserMark> userMarks = mongoTemplate.find(query, UserMark.class, collectionName);
        return userMarks;
    }

    @Override
    public int updateSolved(long comment_id) {
        Criteria criteria=Criteria.where("comment_id").is(comment_id);
        Query query=new Query(criteria);
        UserMark userMark = mongoTemplate.findOne(query, UserMark.class, "user_mark");
        Update update=new Update().set("isSolved",!userMark.isSolved());
        UpdateResult updateResult = mongoTemplate.updateFirst(query, update, UserMark.class, "user_mark");
        int modifiedCount = (int) updateResult.getModifiedCount();
        return modifiedCount;

    }

    @Override
    public boolean getSolved(long comment_id){
        Criteria criteria=Criteria.where("comment_id").is(comment_id);
        Query query=new Query(criteria);
        UserMark userMark = mongoTemplate.findOne(query, UserMark.class, "user_mark");
        return userMark.isSolved();
    }

    @Override
    public int deleteUserMark(long comment_id) {
        Criteria criteria=Criteria.where("comment_id").is(comment_id);
        Query query=new Query(criteria);
        DeleteResult user_mark = mongoTemplate.remove(query, UserMark.class, "user_mark");
        int deletedCount = (int) user_mark.getDeletedCount();
        return deletedCount;
    }

    @Override
    public int addResult(String date) {
        Criteria criteria=Criteria.where("date").is(date);
        Query query=new Query(criteria);
        Result result = mongoTemplate.findOne(query, Result.class, "result");  //如果没有相应的时间就新建一个
        if (result==null){                       //没有这个日期的记录
            Result result2=new Result();
            List<Result> resultList = mongoTemplate.findAll(Result.class, "result");
            long result_id = resultList.size(); //最后一条result_id
            Criteria criteriaLast=Criteria.where("result_id").is(result_id);
            Query queryLast=new Query(criteriaLast);
            Result resultLast=mongoTemplate.findOne(queryLast,Result.class,"result");
            result2.setResult_id(result_id+1);
            result2.setDate(date);
            result2.setOneNum(resultLast.getOneNum());
            result2.setTwoNum(resultLast.getTwoNum());
            result2.setThreeNum(resultLast.getThreeNum());
            result2.setFiveNum(resultLast.getFiveNum());
            result2.setFiveNum(resultLast.getFiveNum());
            result2.setSumNum(resultLast.getSumNum());
            Result insert = mongoTemplate.insert(result2, "result");
            if (insert!=null){
                return 1;
            }else {
                return 0;
            }
        }
        else{
            return 0;
        }
    }

    @Override
    public boolean haveResult(String date) {
        Criteria criteria=Criteria.where("date").is(date);
        Query query=new Query(criteria);
        Result result = mongoTemplate.findOne(query, Result.class, "result");
        if (result==null){
            return false;
        }
        else{
            return true;
        }
    }

    @Override
    public int updateResult(String date,int beforeValue, int afterValue) {
        Criteria criteria=Criteria.where("date").is(date);
        Query query=new Query(criteria);
        Result result = mongoTemplate.findOne(query, Result.class, "result");
        Result result1=new Result();
        if (beforeValue==0&&afterValue==1){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()+1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()+1);
        }
        else if (beforeValue==0&&afterValue==2){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()+1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()+1);
        }
        else if (beforeValue==0&&afterValue==3){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()+1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()+1);
        }
        else if (beforeValue==0&&afterValue==4){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()+1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()+1);
        }
        else if (beforeValue==0&&afterValue==5){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()+1);
            result1.setSumNum(result.getSumNum()+1);
        }
        else if (beforeValue==1&&afterValue==0){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()-1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()-1);

        }
        else if (beforeValue==1&&afterValue==2){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()-1);
            result1.setTwoNum(result.getTwoNum()+1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());

        }
        else if (beforeValue==1&&afterValue==3){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()-1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()+1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());

        }
        else if (beforeValue==1&&afterValue==4){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()-1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()+1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());

        }
        else if (beforeValue==1&&afterValue==5){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()-1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()+1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==2&&afterValue==0){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()-1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()-1);
        }
        else if (beforeValue==2&&afterValue==1){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()+1);
            result1.setTwoNum(result.getTwoNum()-1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==2&&afterValue==3){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()-1);
            result1.setThreeNum(result.getThreeNum()+1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==2&&afterValue==4){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()-1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()+1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==2&&afterValue==5){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()-1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()+1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==3&&afterValue==0){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()-1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()-1);
        }
        else if (beforeValue==3&&afterValue==1){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()+1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()-1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==3&&afterValue==2){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()+1);
            result1.setThreeNum(result.getThreeNum()-1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==3&&afterValue==4){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()-1);
            result1.setFourNum(result.getFourNum()+1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==3&&afterValue==5){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()-1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()+1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==4&&afterValue==0){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()-1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum()-1);
        }
        else if (beforeValue==4&&afterValue==1){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()+1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()-1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==4&&afterValue==2){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()+1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()-1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==4&&afterValue==3){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()+1);
            result1.setFourNum(result.getFourNum()-1);
            result1.setFiveNum(result.getFiveNum());
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==4&&afterValue==5){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()-1);
            result1.setFiveNum(result.getFiveNum()+1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==5&&afterValue==0){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()-1);
            result1.setSumNum(result.getSumNum()-1);
        }
        else if (beforeValue==5&&afterValue==1){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum()+1);
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()-1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==5&&afterValue==2){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum()+1);
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()-1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==5&&afterValue==3){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum()+1);
            result1.setFourNum(result.getFourNum());
            result1.setFiveNum(result.getFiveNum()-1);
            result1.setSumNum(result.getSumNum());
        }
        else if (beforeValue==5&&afterValue==4){
            result1.setDate(date);
            result1.setOneNum(result.getOneNum());
            result1.setTwoNum(result.getTwoNum());
            result1.setThreeNum(result.getThreeNum());
            result1.setFourNum(result.getFourNum()+1);
            result1.setFiveNum(result.getFiveNum()-1);
            result1.setSumNum(result.getSumNum());
        }
        else {
            return 0;
        }
        mongoTemplate.remove(query,Result.class,"result");
        List<Result> result2 = mongoTemplate.findAll(Result.class, "result");
        int size = result2.size();
        result1.setResult_id(size+1);
        Result insert = mongoTemplate.insert(result1, "result");
        if (insert==null){
            return 0;
        }
        else {
            return 1;
        }
    }

    @Override
    public Result getResultByDate(String date) {
        Criteria criteria=new Criteria().where("date").is(date);
        Query query=new Query(criteria);
        Result result = mongoTemplate.findOne(query, Result.class, "result");
        return result;
    }


}
